import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { IResponse } from 'src/interfaces/response.interface';
import { RecordDocument } from 'src/schemas/record.schema';
import { to } from 'src/utils/to';
import { CreateRecordDto } from './dto/create-record.dto';
import { UpdateRecordDto } from './dto/update-record.dto';

const logger = new Logger('record.service');

@Injectable()
export class RecordService {
  private response: IResponse;
  constructor(
    @InjectModel('Record') private recordModel: Model<RecordDocument>,
  ) {}

  create(createRecordDto: CreateRecordDto) {
    return 'This action adds a new record';
  }

  async findAll() {
    const [err, data] = await to(this.recordModel.find({}));
    if (err) {
      logger.error(`发生的一些的错误${err}`);
      this.response = {
        code: 2,
        msg: `查找所有出库记录失败${err}`,
      };
    } else if (data) {
      return data;
    }
  }

  findOne(id: number) {
    return `This action returns a #${id} record`;
  }

  update(id: number, updateRecordDto: UpdateRecordDto) {
    return `This action updates a #${id} record`;
  }

  remove(id: number) {
    return `This action removes a #${id} record`;
  }
}
